package cn.how2go.net.server.protocol;

import cn.how2go.net.server.util.BinaryUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class JT_0704 implements JTMessage {
    private static Logger logger = LoggerFactory.getLogger(JT_0704.class);
    /**
     * 定位包数量
     */
    private short positionPacketNum;
    /**
     * 汇报数据类型，0 正常批量位置汇报 1 盲区补报
     */
    private byte reportType;

    /**
     * 补发的定位包
     */
    private List<JT_0200> positionReportList;

    @Override
    public byte[] write() {
        ByteBuf directBuf = Unpooled.directBuffer(16);
        try {
            directBuf.writeShort(positionPacketNum);
            directBuf.writeByte(reportType);
            for(JT_0200 bean : positionReportList){
                directBuf.writeShort(bean.getBodyLength());
                directBuf.writeInt(bean.getAlarmFlag());
                directBuf.writeInt(bean.getStatus());
                directBuf.writeInt(bean.getLatitude());
                directBuf.writeInt(bean.getLongitude());
                directBuf.writeShort(bean.getAltitude());
                directBuf.writeShort(bean.getSpeed());
                directBuf.writeShort(bean.getCourse());
                directBuf.writeBytes(BinaryUtil.getByteFromString(bean.getTime(),"yyyy-MM-dd HH:mm:ss","yyMMddHHmmss"));
                directBuf.writeBytes(bean.getExpand());
            }
        } catch (Exception e) {
            logger.error("<<<<组包失败");
            directBuf.release();
            return null;
        }

        byte[] bytess = new byte[directBuf.readableBytes()];
        directBuf.readBytes(bytess);
        directBuf.release();
        return bytess;
    }

    @Override
    public void read(byte[] bytes) {
        ByteBuf directBuf = Unpooled.directBuffer(16);
        directBuf.writeBytes(bytes);
        try {
            this.positionPacketNum = directBuf.readShort();
            this.reportType = directBuf.readByte();
            positionReportList = new ArrayList<>();
            for(short i=0;i<this.positionPacketNum;i++) {
                JT_0200 jt0200 = new JT_0200();
                short bodyLength = directBuf.readShort();
                int alarmFlag = directBuf.readInt();
                int status = directBuf.readInt();
                int latitude = directBuf.readInt();
                int longitude = directBuf.readInt();
                short altitude = directBuf.readShort();
                short speed = directBuf.readShort();
                short course = directBuf.readShort();
                String time = "20" + BinaryUtil.bcd2Str(directBuf.readByte()) + "-"
                        + BinaryUtil.bcd2Str(directBuf.readByte()) + "-"
                        + BinaryUtil.bcd2Str(directBuf.readByte()) + " "
                        + BinaryUtil.bcd2Str(directBuf.readByte()) + ":"
                        + BinaryUtil.bcd2Str(directBuf.readByte()) + ":"
                        + BinaryUtil.bcd2Str(directBuf.readByte());
                byte[] expand = new byte[bodyLength - 28];
                directBuf.readBytes(expand);
                jt0200.setAlarmFlag(alarmFlag);
                jt0200.setStatus(status);
                jt0200.setLatitude(latitude);
                jt0200.setLongitude(longitude);
                jt0200.setAltitude(altitude);
                jt0200.setSpeed(speed);
                jt0200.setCourse(course);
                jt0200.setTime(time);
                jt0200.setExpand(expand);
                positionReportList.add(jt0200);
            }
        } catch (Exception e) {
            logger.error("<<<<解包失败" + BinaryUtil.ToHexFormatString(bytes));
            directBuf.release();
        }
        directBuf.release();
    }

    @Override
    public short getBodyLength() {
        short len = 3;
        for(JT_0200 bean : positionReportList){
            len += bean.getBodyLength();
        }
        return len;
    }

    @Override
    public String toString() {
        return "JT_0704{" +
                "positionPacketNum=" + positionPacketNum +
                ", reportType=" + reportType +
                ", positionReportList=" + positionReportList +
                '}';
    }
}
